package com.ruoyi.common.cmb;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.cmb.entity.BB1PAYOPOneParam;
import com.ruoyi.common.cmb.entity.BB1PAYOPTwoParam;
import com.ruoyi.common.cmb.entity.BankSMConfigDto;
import com.ruoyi.common.cmb.enums.CMBTransAction;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 招商银行银企直联国密免前置/SaaS对接示例，本示例仅供参考，不保证各种异常场景运行，请勿直接使用，
 * 如有错漏请联系对接人员。运行时，请使用所获取的测试资源替换 用户编号、公私钥、对称密钥、服务商编号等信息。
 */
public class ApiDemo {
    private static final int BOUND_START = 1000000;
    private static final int BOUND_END = 9000000;
    // 测试地址，生产需要替换
    private static String url = "http://cdctest.cmburl.cn:80/cdcserver/api/v2";
    // 生产地址
    //private static String url = "https://cdc.cmbchina.com/cdcserver/api/v2";
    // 银行公钥
    private static String publicKey = "BNsIe9U0x8IeSe4h/dxUzVEz9pie0hDSfMRINRXc7s1UIXfkExnYECF4QqJ2SnHxLv3z/99gsfDQrQ6dzN5lZj0=";
    // 客户私钥，生产需要替换
    private static String privateKey = "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E=";
    // 对称密钥，生产需要替换
    private static String symKey = "VuAzSWQhsoNqzn0K";

    // 企业网银用户号，生产需要替换
    private static String uid = "U006072203";

    private static Random random = new Random();

    private ApiDemo() {
    }

    public static void main(String[] args) throws GeneralSecurityException, IOException, CryptoException {
        /**
         * 原始接口调用方式
         */
        // 装载BC库,必须在应用的启动类中调用此函数
        Security.addProvider(new BouncyCastleProvider());

        // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
        String funcode = "DCLISMOD";
        // 准备接口数据，生产请替换为具体接口请求报文,包含所需的请求字段
        String currentDatetime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String reqid = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date()) + (BOUND_START + random.nextInt(BOUND_END));
//        String reqid = "123456789101112121abcdacd";
        String data = "{\"request\":{\"body\":{\"buscod\":\"N02030\"},\"head\":{\"funcode\":\"" + funcode + "\",\"userid\":\"" + uid + "\",\"reqid"
            + "\":\"" + reqid + "\"}},\"signature\":{\"sigdat\":\"__signature_sigdat__\",\"sigtim\":\"" + currentDatetime + "\"}}";

        DcHelper dchelper = new DcHelper(url, uid, privateKey, publicKey, symKey);
//        String response = dchelper.sendRequest(data, funcode);
//        process("Api请求1成功,响应报文:\r\n" + response);
        String response2 = dchelper.sendRequest(data, funcode);
        /**
         * response2 = {"response":{"body":{"ntqmdlstz":[{"busmod":"S3013","modals":"有审批"},{"busmod":"S3010","modals":"支付自动标准模式"}]},
         * "head":{"bizcode":"","funcode":"DCLISMOD","reqid":"2025021017562508999717392","resultcode":"SUC0000","resultmsg":"","rspid":"202502101756267980001cdcserver-76f6fccb6d-6l8jk","userid":"U006072203"}},
         * "signature":{"sigdat":"eRq3PZzCB8kcr9X+5knMyD6rc3JuihhEQCkWsh5EDqBOIR+6SRztiyZaP3H6CvH6a2hrVFDOS/8e406JKSUPuw==","sigtim":"20250210175626"}}
         */
        System.out.println("data = " + data);
        System.out.println("response2 = " + response2);
//        process("Api请求2成功,响应报文:\r\n" + response2);
    }

    private static void process(String response) {

    }
}
